/**
 * <p><b>Title:</b> RuleManager.java </p>
 *
 * @Package com.wondertek.oes.charge.service.manager
 */
package com.wondertek.poms.sync.manager.filter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.core.filter.RuleFilterChain;
import com.wondertek.poms.core.filter.impl.AssertLevelFilter;
import com.wondertek.poms.core.filter.impl.AssistFilter;
import com.wondertek.poms.core.filter.impl.AuthorizationWayFilter;
import com.wondertek.poms.core.filter.impl.ContentTypeFilter;
import com.wondertek.poms.core.filter.impl.CopyRightBlackAreaFilter;
import com.wondertek.poms.core.filter.impl.CopyRightCpIdFilter;
import com.wondertek.poms.core.filter.impl.CopyRightOptimalFilter;
import com.wondertek.poms.core.filter.impl.CopyRightScopeFilter;
import com.wondertek.poms.core.filter.impl.CopyRightTerminalFilter;
import com.wondertek.poms.core.filter.impl.CopyRightTypeFilter;
import com.wondertek.poms.core.filter.impl.CopyRightWhiteAreaFilter;
import com.wondertek.poms.core.filter.impl.FirClassifyFilter;
import com.wondertek.poms.core.filter.impl.FormTypeFilter;
import com.wondertek.poms.core.filter.impl.FreeTypeFilter;
import com.wondertek.poms.core.filter.impl.HasVideoFilter;
import com.wondertek.poms.core.filter.impl.MediaUsageCodeFilter;
import com.wondertek.poms.core.filter.impl.NeedDRMFilter;
import com.wondertek.poms.core.filter.impl.OutputFilter;
import com.wondertek.poms.core.filter.impl.RecommendationFilter;
import com.wondertek.poms.core.filter.impl.SecondClassifyFilter;
import com.wondertek.poms.core.service.IPrdRuleSecondClassifyService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdInfoRule;
import com.wondertek.poms.dao.po.PrdRuleSecondClassify;

import lombok.extern.slf4j.Slf4j;

/**
 * <p><b>RuleManager Description:</b> 对已选规则、限制规则等操作的类,使用过滤链实现业务</p>
 *
 * @author douzi
 * <b>DATE</b> 2019年2月28日 上午9:39:49
 */
@Slf4j
@Component
public class RuleManager {
	
	@Autowired
	private IPrdRuleSecondClassifyService prdRuleSecondClassifyService;
	
    /**
     * <p><b>Title:</b> contentFilter</p>
     * <p><b>Description:</b> 商品包规则过滤</p>
     *
     * @param rule    商品包规则
     * @param vo      接收的内容vo
     * @param isLimit 是否是限制规则
     * @return
     * @author douzi
     */
    public boolean contentFilter(PrdInfoRule rule, Content c, boolean isLimit) {
    	setSecondClassifys(rule);
    	
        RuleFilterChain rfc = new RuleFilterChain();
        if (isLimit) {
            rfc.addFilter(new CopyRightBlackAreaFilter());	//版权地域范围 黑名单
        } else {
            rfc.addFilter(new CopyRightWhiteAreaFilter())	//版权地域范围 白名单
            	.addFilter(new MediaUsageCodeFilter());		//码率
        }
        rfc.addFilter(new CopyRightCpIdFilter())    		//版权所属CP
                .addFilter(new AssistFilter())				//辅助分类
                .addFilter(new OutputFilter())				//是否输出
                .addFilter(new ContentTypeFilter())			//内容类别
                .addFilter(new FreeTypeFilter())			//资费类型
                .addFilter(new CopyRightOptimalFilter())	//版权最优
                .addFilter(new FirClassifyFilter())			//内容一级分类
                .addFilter(new CopyRightTypeFilter())		//版权类型
                .addFilter(new CopyRightTerminalFilter())	//版权终端
                .addFilter(new HasVideoFilter())			//是否可无媒体文件
                .addFilter(new CopyRightScopeFilter())		//版权授权范围
                .addFilter(new FormTypeFilter())          	//剧集类型
                .addFilter(new NeedDRMFilter())				//数字版权保护
                .addFilter(new AssertLevelFilter())			//内容评级
                .addFilter(new RecommendationFilter())		//推荐标签
                .addFilter(new CopyRightScopeFilter())		//版权授权范围
                .addFilter(new AuthorizationWayFilter())	//授权方式
                .addFilter(new SecondClassifyFilter())		//二级分类
                .doFilter( rule, c, rfc);

        if (rfc.isFlag()) {
            log.info("商品包规则|内容id:" + c.getContentId() + "|assetId:" + c.getAssetId() + "|商品包ID:" + rule.getPrdInfoId() + "|" + (isLimit ? "商品包限制规则" : "商品包已选规则") + "|匹配成功|copyRightObjectID:" + c.getCopyrightObjectId() + "|" + rule.toString());
        } else {
            log.info("商品包规则|内容id:" + c.getContentId() + "|assetId:" + c.getAssetId() + "|商品包ID:" + rule.getPrdInfoId() + "|" + (isLimit ? "商品包限制规则" : "商品包已选规则") + "|匹配失败|copyRightObjectID:" + c.getCopyrightObjectId() + "|" + rfc.getResultStr().toString());
        }
        return rfc.isFlag();
    }

	/**
	 * <p><b>Title:</b> setSecondClassifys</p>
	 * <p><b>Description:</b> 设置二级分类列表值进规则对象 </p>
	 * @author douzi
	 * @param rule
	 */
	private void setSecondClassifys(PrdInfoRule rule) {
		PrdRuleSecondClassify params = new PrdRuleSecondClassify();
    	params.setPrdRuleId(rule.getPrdRuleId());
    	List<PrdRuleSecondClassify> dbprsc = prdRuleSecondClassifyService.findAll(params);
    	Set<PrdRuleSecondClassify> set = new HashSet<>(dbprsc);
    	rule.setSecondClassifys(set);
	}
}
